
    /**  
    * @Title: ErrorFilter.java
    * @Package com.efast.cafe.gateway.filter
    * @Description: TODO(用一句话描述该文件做什么)
    * @author Administrator
    * @date 2018年8月21日
    * @version V1.0  
    */
    
package com.efast.cafe.gateway.filter;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

import com.efast.cafe.gateway.bean.ResultBean;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;


/**
    * @ClassName: ErrorFilter
    * @Description: TODO(这里用一句话描述这个类的作用)
    * @author Administrator
    * @date 2018年8月21日
    *
    */

public class ErrorFilter extends ZuulFilter{

	private static final String ERROR_STATUS_CODE_KEY = "error.status_code";

    

    public static final String DEFAULT_ERR_MSG = "系统繁忙,请稍后再试";

    @Override
    public String filterType() {
        return "post";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        RequestContext ctx = RequestContext.getCurrentContext();
        boolean res = ctx.containsKey(ERROR_STATUS_CODE_KEY);
        return true;
    }

    @Override
    public Object run() {       
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        HttpServletResponse response = ctx.getResponse();
        try {
           
             Throwable e = RequestContext.getCurrentContext().getThrowable();
             String message = "";
                Throwable re = getOriginException(e);
                if(re instanceof java.net.ConnectException){
                    message = "Real Service Connection refused";
                   // log.warn("uri:{},error:{}" ,request.getRequestURI(),re.getMessage());
                }else if(re instanceof java.net.SocketTimeoutException){
                    message = "Real Service Timeout";
                   // log.warn("uri:{},error:{}" ,request.getRequestURI(),re.getMessage());
                }else if(re instanceof com.netflix.client.ClientException){
                    message = re.getMessage();
                   // log.warn("uri:{},error:{}" ,request.getRequestURI(),re.getMessage());
                }else{
                   // log.warn("Error during filtering",e);
                }
            
            ResultBean res = new ResultBean(false,message);
            res.setErrorCode("500");
         
            response.setCharacterEncoding("UTF-8");
    		response.setContentType("application/json; charset=utf-8");
    		response.getWriter().print(res.toString());
          //  WebUtils.responseOutJson(ctx.getResponse(), res);
        } catch (Exception e) {
            String error = "Error during filtering[ErrorFilter]";
            ResultBean res = new ResultBean(false,error);
            res.setErrorCode("500");
         
            response.setCharacterEncoding("UTF-8");
    		response.setContentType("application/json; charset=utf-8");
    		try {
				response.getWriter().print(res.toString());
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
        }
        return null;

    }

    private Throwable getOriginException(Throwable e){
        e = e.getCause();
        while(e.getCause() != null){
            e = e.getCause();
        }
        return e;
    }
}
